package com.jingan.nursinghome.regulatory.select.timu;

import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter;
import com.jingan.nursinghome.regulatory.dialog.HostoryDialog;
import com.jingan.nursinghome.regulatory.dialog.XuantiDialog;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.select.timu.adapter.MicrometerTableAdapterT2;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.MySpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.UtilToast;

/**
 * 千分表，评分详情页
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableOSDialog2 extends BaseDialog {
    public static int pageIndex = 1;//当前页数。从1开始。题号。
    public static int count = 3;
    private static int maxPage;//最大页

    public static int getMaxPage() {
        maxPage = (int) Math.ceil((double) mtopics.size() / (double) (count - 1));
        return maxPage;
    }


    private MicrometerTableOSDialog2(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_micrometertablleos, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        BaseApplication.getInstance().setStatusBarDrak(window, true);
        initUI();
        adapterUI();
    }

    public static MicrometerTableOSDialog2 micrometerTableOSDialog;
    public static ArrayList<Topic> mtopics;//千分表数据
    static String iid;//巡检信息id

    public static MicrometerTableOSDialog2 getInstance(BaseActivity baseActivity, ArrayList<Topic> topics, String id) {
        mtopics = topics;
        iid = id;
        if (isFinishing(micrometerTableOSDialog)) {
            micrometerTableOSDialog = new MicrometerTableOSDialog2(baseActivity);
        } else {
            micrometerTableOSDialog.show();
        }
        return micrometerTableOSDialog;
    }

    MyActionBar myActionBar;

    TextView txt_hostory, txt_select, txt_next, txt_next2;

    RecyclerView recyclerView;

    public String tihao = "";


    TextView txt_hot;

    void initHot() {
        if (txt_hot == null) {
            txt_hot = (TextView) findViewById(R.id.txt_hot);
            ProportionUtils.getInstance().adapterTextView(txt_hot);
        }
        txt_hot.setVisibility(View.INVISIBLE);//查看，没有历史整改
    }

    void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "千分评分系统");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                onBack();
            }
        });
//        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK
//                        && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    onBack();
//                    return true;//记得返回true。表示已经处理。
//                }
//                return false;//返回键以外交给系统自行处理。不可以屏蔽，不然输入法键盘的按键可能无效。如删除键
//            }
//        });
        txt_hostory = (TextView) findViewById(R.id.txt_hostory);
        txt_hostory.setText("返回");//没有历史整改
        txt_select = (TextView) findViewById(R.id.txt_select);
        txt_next = (TextView) findViewById(R.id.txt_next);
        txt_next2 = (TextView) findViewById(R.id.txt_next2);//這個隱藏。就有历史整改
        txt_next2.setVisibility(View.INVISIBLE);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.txt_hostory://历史整改
//                        if (mHostoryTopics.size() > 0) {
//                            HostoryDialog.getInstance(baseActivity, mHostoryTopics);
//                        } else {
//                            UtilToast.showToastView("暂无历史整改");
//                        }
                        dismiss();
                        break;
                    case R.id.txt_select://选题
                        XuantiDialog2.getInstance(baseActivity, mtopics, pageIndex, new BaseListenner<Integer>() {
                            @Override
                            public void onResult(Integer result) {
                                //返回当前页面
                                pageIndex = result;
                                //recyclerView.smoothScrollToPosition(pageIndex);
                                //linearLayoutManager.scrollToPosition(pageIndex);
                                linearLayoutManager.scrollToPositionWithOffset(pageIndex - 1, 0);
                            }
                        });
                        break;
                    case R.id.txt_next://下一题
                    case R.id.txt_next2:

                        if (pageIndex >= mtopics.size() || linearLayoutManager.findLastCompletelyVisibleItemPosition() == (mtopics.size() - 1)) {
                            //最后一页
                            //fixme ======================================================跳转到提交页
                            //CommitDailog.getInstance(baseActivity, micrometerTableOSDialog, maxScore + "", score + "");
                            MicrometerTableResultDialog.getInstance(baseActivity, iid,MicrometerTableOSDialog2.this);
                            break;
                        }

                        pageIndex += 2;
                        if (pageIndex >= mtopics.size()) {
                            pageIndex = mtopics.size();
                        }
                        canScrollVertically = true;
                        recyclerView.smoothScrollToPosition(pageIndex);
                        break;
                }
            }
        };
        txt_hostory.setOnClickListener(onClickListener);
        txt_select.setOnClickListener(onClickListener);
        txt_next.setOnClickListener(onClickListener);
        txt_next2.setOnClickListener(onClickListener);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity) {
            @Override
            public boolean canScrollVertically() {
                //return canScrollVertically;
                return true;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new MySpacesItemDecoration(baseActivity, 0));
        //recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                    //int postion = linearLayoutManager.findFirstVisibleItemPosition();
                    int postion = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                    pageIndex = postion + 1;
                    linearLayoutManager.scrollToPositionWithOffset(pageIndex - 1, 0);
                    canScrollVertically = false;
                }
            }
        });
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    canScrollVertically = false;
                }
                return false;
            }
        });
    }

    boolean canScrollVertically = false;
    LinearLayoutManager linearLayoutManager;

    AlertDialog alertDialog;

    void onBack() {
        alertDialog = AlertDialog.getInstance(baseActivity, false, true).setMession("是否退出本次评分?");
        alertDialog.setPositive("是", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                alertDialog.dismiss();
            }
        });
    }

    void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(txt_hostory);
        ProportionUtils.getInstance().adapterTextView(txt_select);
        ProportionUtils.getInstance().adapterTextView(txt_next);
        ProportionUtils.getInstance().adapterTextView(txt_next2);
    }

    @Override
    protected void listener() {
        pageIndex = 1;
        recyclerView.setAdapter(new MicrometerTableAdapterT2(baseActivity, mtopics));
        initHot();
    }

    //更新
    public void updateAdapter() {
        recyclerView.setAdapter(new MicrometerTableAdapterT2(baseActivity, mtopics));
    }

    @Override
    protected void recycleView() {

    }
}
